
package cn.nubia.admin.sysconfig.controller;

import cn.nubia.admin.sysconfig.bean.Category;
import cn.nubia.admin.sysconfig.bean.CategoryQuery;
import cn.nubia.admin.sysconfig.service.CategoryService;
import cn.nubia.admin.common.exception.NBException;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>User:nbc/p>
 * <p>Date:15-6-30  上午9:38</p>
 */
@Controller
@RequestMapping("/admin/category")
public class CategoryController  {
    private final static Logger logger = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    private CategoryService categoryService;
    /**
     * 新增分类接口
     *
     * @param category 分类对象
     * @return 新增的分类对象.
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Response<Category>  saveCategory(@Valid Category category,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.getFailedResponse(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            if(category.getId()==null) {
                if (category.getLevel() != null)
                    category.setLevel(category.getLevel() + 1);
                category = categoryService.addCategory(category);
            }else {
                categoryService.updateCategory(category);
            }
        }catch (Exception e) {
            logger.error("保存分类异常:参数:"+category.toString(), e);
            return Response.getFailedResponse("保存分类失败",null);
        }
        return Response.getSuccessResponse(category);
    }
    /**
     * 新增分类接口
     *
     * @param parentId 分类id
     * @return 新增的分类对象.
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
//    @RequiresPermissions(value = "category:add")
    public ModelAndView addCategory(Integer parentId) {
        ModelAndView modelAndView = new ModelAndView("/category/add");
        try {
            Category parentCategory = categoryService.get(parentId);
            modelAndView.addObject("parentCategory",parentCategory);
            modelAndView.addObject("category",new Category());
        }catch (Exception e) {
            logger.error("新增分类异常:参数:"+parentId, e);
        }
        return modelAndView;
    }
    /**
     * 查看分类
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
//    @RequiresPermissions(value = "category:edity")
    public ModelAndView view(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/category/add");
        Category category =null;
        try {
            category =  categoryService.viewCategory(id);
            if(category.getParentId()!=null&&category.getParentId()!=0){
              Category   parentCategory =  categoryService.viewCategory(category.getParentId());
              modelAndView.addObject("parentCategory",parentCategory);
            }
            modelAndView.addObject("category",category);
        }  catch (Exception e) {
            logger.error("查看分类异常:参数"+id, e);
        }
        return modelAndView;
    }

    /**
     * 分类列表
     * @param categoryQuery
     * @return
     *
     */
    @RequiresPermissions(value = "soft:app:category:list")
    @RequestMapping(value ="/list", method = RequestMethod.GET)
    public ModelAndView list(CategoryQuery categoryQuery) {
        //参数不正确
            Paged<Category> categoryPaged=null;
        ModelAndView modelAndView = new ModelAndView("/category/list");
        try {
            categoryQuery.setParentId(0);
            categoryPaged = categoryService.list(categoryQuery);
            modelAndView.addObject("pageList",categoryPaged);
            modelAndView.addObject("query",categoryQuery);
        }  catch (Exception e) {
            logger.error("分类查询异常:参数:"+categoryQuery.toString(), e);
           // return Response.getFailedResponse("分类查询失败", null);
        }
        return modelAndView;
    }

    /**
     * 分类列表
     * @param categoryQuery
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/listCategies")
    public Response<List<Category>>  listCategies(CategoryQuery categoryQuery) {
        //参数不正确
        List<Category> categories=null;
        try {
            Paged<Category> categoryPaged = categoryService.list(categoryQuery);
            categories = categoryPaged.getListData();
        }  catch (Exception e) {
            logger.error("分类查询异常:参数:"+categoryQuery.toString(), e);
             return Response.getFailedResponse("分类查询失败", null);
        }
        return Response.getSuccessResponse(categories);
    }
    /**
     * 分类列表
     * @param categoryQuery
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/categoryList", method = RequestMethod.GET)
    public Response<List<Category>> categoryList(CategoryQuery categoryQuery) {
        //参数不正确
        List<Category> categoryList=null;
        try {
            categoryList = categoryService.categoryList(categoryQuery);
        }  catch (Exception e) {
            logger.error("分类列表异常:参数:"+categoryQuery.toString(), e);
             return Response.getFailedResponse("分类列表查询失败", null);
        }
        return Response.getSuccessResponse(categoryList);
    }
    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
//    @RequiresPermissions(value = "category:delete")
    public Response<Boolean> delete(Integer id) {
        boolean result=false;
        try {
           result= categoryService.deleteCategory(id);
        }  catch (NBException e) {
            logger.info(e.getMessage());
            return Response.getFailedResponse(e.getMessage(),false);
        } catch (Exception e) {
            logger.error("删除分类异常:参数"+id, e);
            return Response.getFailedResponse("删除失败",false);
        }
        return Response.getSuccessResponse(true);
    }

    /**
     * 显示隐藏分类
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/state", method = RequestMethod.POST)
    @ResponseBody
    public Response<Boolean> show(Integer id,Integer state) {
        //参数不正确
        if (id == null) {
            return Response.getFailedResponse("要显示隐藏分类的id不能为空", false);
        }
        boolean result;
        try {
            result=categoryService.updateState(id,state);
        }  catch (Exception e) {
            logger.error("显示隐藏分类异常:参数"+id, e);
            return Response.getFailedResponse("显示隐藏分类失败", false);
        }
        return Response.getSuccessResponse(true);
    }
    /**
     * 校验分类
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ResponseBody
    public Response<Boolean> check(Integer id,Integer parentId,String categoryName) {
        //参数不正确
        if (id == null) {
            return Response.getFailedResponse("要显示隐藏分类的id不能为空", false);
        }
        boolean result;
        try {
            result=categoryService.check(id,parentId,categoryName);
        }  catch (Exception e) {
            logger.error("显示隐藏分类异常:参数"+id, e);
            return Response.getFailedResponse("显示隐藏分类失败", false);
        }
        return Response.getSuccessResponse(result);
    }
}
